package cn.pzaily.orange.manage.security;

import cn.pzaily.orange.manage.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: cn.pzaily.youpinxiu.acl.filter
 * @Description: token过滤器，验证token
 * @Author: Pang
 * @Date: 2021-03-05 23:27
 */
public class JwtAuthenticationTokenFilter extends BasicAuthenticationFilter {

    private JwtTokenUtils jwtTokenUtil;

    public JwtAuthenticationTokenFilter(AuthenticationManager authenticationManager, JwtTokenUtils tokenUtils) {
        super(authenticationManager);
        this.jwtTokenUtil = tokenUtils;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        SecurityContextHolder.getContext().setAuthentication(jwtTokenUtil.getAuthenticationFromToken(request));
        chain.doFilter(request, response);
    }

}
